package day22;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

/**
 * @author ZJX
 * @version 1.0
 * @descption 单调栈
 * @since 2025/11/1 10:11
 */
public class MonotonicStack {

    public int[] nextGreaterElementI(int[] nums1, int[] nums2) {
        int n = nums1.length;
        int[] ret = new int[n];
        Stack<Integer> stack = new Stack<>();
        Map<Integer, Integer> map = new HashMap<>(); // k : nums[i]  v: 下一个比nums[i]大的元素
        for (int num : nums2) {
            while (!stack.isEmpty() && num > stack.peek()) {
                int top = stack.pop();
                map.put(top, num);
            }
            stack.push(num);
        }
        while (!stack.isEmpty()) {
            map.put(stack.pop(), -1);
        }
        for (int i = 0; i < nums1.length; i++) {
            ret[i] = map.get(nums1[i]);
        }
        return ret;
    }

    public int[] nextGreaterElementsII(int[] nums) {
        int n = nums.length;
        int[] ret = new int[n];

        Arrays.fill(ret, -1);
        Stack<Integer> stack = new Stack<>(); // 栈存索引（原数组的索引，0~n-1）

        for (int i = 0; i < 2 * n; i++) {
            int cur = nums[i % n];
            while (!stack.isEmpty() && cur > nums[stack.peek()]) {
                int topIndex = stack.pop();
                ret[topIndex] = cur;
            }
            if (i < n) {
                stack.push(i);
            }
        }
        return ret;
    }


    public int[] dailyTemperatures(int[] temperatures) {
        Stack<Integer> stack = new Stack<>();  // 存索引下标 算出相对位置
        int n = temperatures.length;
        int[] ret = new int[n];
        stack.push(temperatures[0]);
        for (int i = 1; i < n; i++) {
            while (!stack.isEmpty() && temperatures[i] > temperatures[stack.peek()]) {
                int index = stack.pop();
                ret[index] = i - index;
            }
            stack.push(i);
        }
        return ret;
    }

//    public int largestRectangleArea(int[] heights) {
        // todo 明天做
//    }

    public int trapI(int[] height) {
        //  双指针解法
        if (height == null || height.length < 2) {
            return 0; // 少于2根柱子无法形成凹槽，接水量为0
        }
        int left = 0, right = height.length - 1;
        int leftMax = 0, rightMax = 0;
        int ret = 0;
        while (right > left) {
            if (height[left] < height[right]) {
                if (height[left] > leftMax) {
                    leftMax = height[left];
                } else {
                    ret += leftMax - height[left];
                }
                left++;
            } else {
                if (height[right] > rightMax) {
                    rightMax = height[right];
                } else {
                    ret += rightMax - height[right];
                }
                right--;
            }
        }
        return ret;
    }

    public int trapII(int[] height) {
        // 动态规划解法
        int n = height.length;
        int[] left = new int[n];
        int[] right = new int[n];
        int total = 0;

        left[0] = height[0];
        for (int i = 1; i < n; i++) {
            left[i] = Math.max(left[i - 1], height[i]);
        }

        right[n - 1] = height[n - 1];
        for (int i = n - 2; i >= 0; i--) {
            right[i] = Math.max(right[i + 1], height[i]);
        }

        for (int i = 0; i < n; i++) {
            int cur = Math.min(left[i], right[i]) - height[i];
            total += Math.max(cur, 0);
        }
        return total;
    }
}





